package com.toogu.mazhangmen.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.toogu.mazhangmen.MyApplication;
import com.toogu.mazhangmen.R;


public class UiUtil {
    private static Context context= MyApplication.getContext();

    //从dip值转换为pix像素值，代码中用的是pix像素值，xml文件中用的是dip值
    public static int dip2px(float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }

    public static boolean isPad() {
//        Point point=getWindowPoint();
//        return point.x>=point.y;
        return ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);
    }

    public static int getMonitorNumber() {
        return isPad()?12:8;
    }

    public static Point getWindowPoint(){
        Display display=((WindowManager) (context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE))).getDefaultDisplay();
        Point point=new Point();
        display.getSize(point);
        return point;
    }

    //设置背景透明度
    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    public static TextView createText(Context context,String content) {
        TextView textView=new TextView(context);
        textView.setTextColor(context.getResources().getColor(R.color.text_blue));
        textView.setText(content);
        textView.setPadding(UiUtil.dip2px(5),UiUtil.dip2px(5),UiUtil.dip2px(5),0);
        return textView;
    }

    public static Button createButton(Context context,int id, String content, boolean isClickable, View.OnClickListener onClickListener) {
        Button button = new Button(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.weight=1;
        layoutParams.rightMargin = UiUtil.dip2px(2);
        button.setLayoutParams(layoutParams);
        button.setId(id);
        button.setText(content);
        button.setBackgroundResource(R.drawable.bg_control_button);
        button.setOnClickListener(onClickListener);
        if(!isClickable){
            button.setClickable(false);
            button.setBackgroundResource(R.drawable.bg_control_button_unclick);
        }
        return button;
    }


}
